package com.juc.threadPoolExecutor.threadPoolExecutor8;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 方法awaitTermination() 与shutdown()结合时可以实现“等待执行完毕”的效果
 * 原理就是应用awaitTermination()方法具有阻塞性，如果awaitTermination()
 * 方法正在阻塞过程中任务执行完毕，则awaitTermination()取消阻塞继续执行
 * 后面的代码.
 *
 */
public class Test4 {

	public static void main(String[] args) throws InterruptedException {
		MyRunnable1 myRunnable = new MyRunnable1();
		ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 99999, 9999L, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
		pool.execute(myRunnable);
		pool.execute(myRunnable);
		pool.execute(myRunnable);
		pool.execute(myRunnable);
		pool.shutdown();
		System.out.println( pool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS) 
				+ "    " +System.currentTimeMillis() + "  全部任务执行完毕！");
	}
}
